package com.qf.service.impl;

import com.qf.entity.SystemMnue;
import com.qf.entity.UserEmp;
import com.qf.entity.UserRole;
import com.qf.mapper.UserMapper;
import com.qf.service.UserLoginExitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 2019/11/22
 * 郭程鹏
 * gcp1909
 * 面向代码面向君,不负时光不负卿
 */
@Service
public class LoginExitImpl implements UserLoginExitService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserEmp login(String loginNumber) {
        UserEmp userEmp = userMapper.login(loginNumber);
        return userEmp;
    }

    //根据得到全部的菜单
    @Override
    public UserRole getMenu(Integer roleId){
        //登录成功获得角色对象
        UserRole userRole = userMapper.getRole( roleId);
        //得到全部表单
        List<SystemMnue> mnues = userMapper.getMnues(userRole.getRoleId());
        //一级菜单
        ArrayList fristMnues = new ArrayList<>();
        for (SystemMnue mnue : mnues) {

            if (mnue.getSuperMnueId()==0){
                //二级菜单
                ArrayList secondMnues=new ArrayList<>();

                for (SystemMnue smnue : mnues) {
                    //菜单中有上级菜单且相等
                    if (mnue.getMnueId()==smnue.getSuperMnueId()){
                        //保存到二级菜单中
                        secondMnues.add(smnue);
                    }
                }

                //封装一个完整的一二级菜单
                mnue.setSecondMnue(secondMnues);
                //将一个完整的二级菜单存放到一级菜单中
                fristMnues.add(mnue);
            }


        }
        List<SystemMnue> pressions = userMapper.getPressions(userRole.getRoleId());
        //将这个角色拥有的所有菜单赋值
        userRole.setMnueList(fristMnues);
        //将这个对象对应的功能赋值
        userRole.setPressions(pressions);

        return userRole;
    }

}
